/*
 * PlaylistUpAction.java
 *
 * Created on April 2, 2006, 1:13 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.bff.mpd.musicjukebox.lib.actions;

import com.bff.mpd.utils.Utils;
import java.awt.event.ActionEvent;
import javax.swing.Action;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JList;
import org.bff.javampd.MPDPlaylist;
import org.bff.javampd.exception.MPDConnectionException;
import org.bff.javampd.exception.MPDException;
import org.bff.javampd.exception.MPDPlaylistException;
import org.bff.javampd.objects.MPDSong;

/**
 *
 * @author Bill
 */
public class PlaylistUpAction extends PlaylistMoveAction {
    private JList list;
    
    /** Creates a new instance of PlaylistUpAction */
    public PlaylistUpAction(JList list, MPDPlaylist playlist) {
        super(playlist);
        this.list = list;
        putValue(Action.SMALL_ICON, new ImageIcon(getClass().getResource("/resources/mpd/playlistUp_3_small.png")));
        putValue(Action.SHORT_DESCRIPTION, Utils.getMessage("ACTION_MOVE_UP_SHORT_DESC"));
    }
    
    @Override
    public void actionPerformed(ActionEvent evt) {
        DefaultListModel model = (DefaultListModel)list.getModel();
        int index = list.getSelectedIndex();
                
        if( index > 0 ) {
            MPDSong song1 = (MPDSong)model.getElementAt(index);
            MPDSong song2 = (MPDSong)model.getElementAt(index-1);
            try {
                swapSongs(song1, song2);
            } catch (MPDException e) {
                e.printStackTrace();
            }

            Object temp = model.remove( index );
            model.add( index - 1, temp );
            list.setSelectedValue(song1, true);
        }
    }
}